package pers.xx.sbm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import pers.xx.sbm.dao.UserMapper;
import pers.xx.sbm.entity.User;
import pers.xx.sbm.enums.ResultEnum;
import pers.xx.sbm.service.ImportExcelService;
import pers.xx.sbm.service.UserService;
import pers.xx.sbm.utils.ExportExcelUtils;
import pers.xx.sbm.utils.UserUtils;
import pers.xx.sbm.vo.ResResult;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：xiexing
 * @description：用户Controller
 * @date ：2020/8/17 14:18
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    UserMapper userMapper;

    @Autowired
    private UserUtils userUtils;//用户工具

    @Autowired
    private ImportExcelService importExcelService;


    /**
     * @description 用户管理
     * @author XieXing
     * @date 2020/8/11 15:45
     */
    @RequestMapping("/")
    public String userPage(Model model) {
        return "page/system/user";
    }

    /**
     * @description 获取用户列表
     * @author XieXing
     * @date 2020/8/16 12:02
     */
    @RequestMapping("/getUserList")
    @ResponseBody
    public ResResult getUserList(User user, Integer page, Integer limit, String userSearchContent) {
        if (!StringUtils.isEmpty(userSearchContent)) {
            user.setUserName(userSearchContent);
        }
        ResResult resResult = userService.listByPage(user, page, limit);
        resResult.setOptName("获取用户列表");
        return resResult;
    }

    /**
     * @description 保存用户信息
     * @author XieXing
     * @date 2020/8/17 14:22
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResResult saveUser(User user, String roles) {
        ResResult resResult = userService.insertWithRoles(user, roles);
        resResult.setOptName("保存用户信息");
        return resResult;
    }


    /**
     * @description 更新用户信息
     * @author XieXing
     * @date 2020/8/17 22:03
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResResult updateUser(User user, String roles) {
        ResResult resResult = userService.updateWithRoles(user, roles);
        return resResult;
    }

    /**
     * @description 删除用户 逻辑删除
     * @author XieXing
     * @date 2020/8/17 23:02
     */
    @RequestMapping("/{id}")
    @ResponseBody
    public ResResult delUser(@PathVariable(value = "id") Integer id) {
        ResResult resResult = userService.deleteByPrimaryKey(id);
        resResult.setOptName("删除用户信息");
        return resResult;
    }


    /**
     * @description 更新用户状态
     * @author XieXing
     * @date 2020/8/23 21:24
     */
    @RequestMapping("/{id}/{status}")
    @ResponseBody
    public ResResult updateUserStatus(@PathVariable(value = "id") Integer id, @PathVariable(value = "status") Integer status) {
        User currentUser = userUtils.getCurrentUser();
        User user = new User();
        user.setUserUpdateby(currentUser.getUserName());//设置修改人姓名
        user.setUserUpdatebyId(currentUser.getId());//设置修改人id
        user.setId(id);//设置用户id
        user.setUserIsEnabled(status);//设置用户状态
        ResResult resResult = userService.updateByPrimaryKeySelective(user);
        resResult.setOptName("更新用户状态");
        return resResult;
    }


    /**
     * @description 导出用户信息
     * @author XieXing
     * @date 2020/12/10 9:30
     */
    @RequestMapping("/exportUser")
    public void exportUser(HttpServletResponse response) throws Exception {
        List<User> userList = userMapper.getListSelective(null);
        String[] columnNames = {"姓名", "性别", "账号", "电话", "邮箱", "地址"};//组装表头
        List<Object[]> data = new ArrayList<>();
        for (int i = 0; i < userList.size(); i++) {
            User user = userList.get(i);
            //根据表头组装数据
            Object[] tmp = {user.getUserName(), user.getUserGender(), user.getUserAccount(), user.getUserPhone(), user.getUserEmail(), user.getUserAddress()};
            data.add(tmp);
        }
        ExportExcelUtils.exportExcel(data, columnNames, "用户信息", "");
    }

    /**
     * @description 用户信息导入
     * @author XieXing
     * @date 2020/12/10 11:35
     */
    @RequestMapping(value = "/importUser", method = RequestMethod.POST)
    @ResponseBody
    public ResResult importUser(MultipartFile file) {
        long start = System.currentTimeMillis();
        List<User> users = importExcelService.importExcelWithSimple(file);
        long end = System.currentTimeMillis();
        ResResult resResult = new ResResult(ResultEnum.SUCCESS, users);
        resResult.setStateInfo("导入数据为" + users.size() + "条，" + "解析耗时" + (end - start) / 1000.0 + "s，" + "确认导入吗？");
        return resResult;
    }


}
